package userDefThreadPool;

import java.util.LinkedList;

@SuppressWarnings("hiding")
public class UserDefBlockingQueue<Runnable> {
    LinkedList<Runnable> queue;

    public UserDefBlockingQueue() {
        queue = new LinkedList<Runnable>();
    }

    public void enque(final Runnable element) {
        synchronized (this) {
            queue.addLast(element);
            notify();
        }
    }

    public void close() {
        queue = null;
        notifyAll();
    }

    public Runnable deQueue() throws ClosedQueueException, InterruptedException {
        Runnable returnedValue;
        synchronized (this) {
            if (queue.size() < 1) {
                try {
                    wait();
                } catch (final InterruptedException e) {
                    throw e;
                }
            }
            if (queue.size() > 0) {
                returnedValue = queue.removeFirst();
            } else {
                throw new ClosedQueueException();
            }

        }
        return returnedValue;
    }

}
